package com.xb.service;

import com.xb.dao.DeptDao;
import com.xb.dao.UserDao;
import com.xb.entity.Dept;
import com.xb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptService {

    @Autowired
    DeptDao deptDao;

    @Autowired
    UserDao userDao;

    public Map<String, List<User>> findAll(){
        Map<String, List<User>> map = new HashMap<>();
        List<Dept> depts = deptDao.findAll();
        List<User> users = userDao.findAll();
        for (Dept dept : depts) {
            List<User> userList = new ArrayList<>();
            for (User user : users) {
                if (user.getDeptId() == dept.getId()) {
                    userList.add(user);
                }
            }
            map.put(dept.getName(), userList);
        }
        return map;
    }

    public List<Dept> find() {
        return deptDao.findAll();
    }
}
